﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne;

namespace OgilvyOne.SqlServerDAL
{
    public class DALUserInfo : OgilvyOne.IDAL.IDALUserInfo
    {
        public Model.Golf_UserInfo getUser(string sql, params SqlParameter[] paramList)
        {
            Model.Golf_UserInfo user = null;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(sql, paramList))
            {
                if (reader.Read())
                {
                    user = OgilvyOne.Utility.Util.getObject(new Model.Golf_UserInfo(), reader) as Model.Golf_UserInfo;
                }
            }
            return user;
        }
        public IList getUserList(string sql)
        {
            IList userlist = new List<Model.Golf_UserInfo>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(sql, new SqlParameter[0]))
            {
                while (reader.Read())
                {
                    userlist.Add(OgilvyOne.Utility.Util.getObject(new Model.Golf_UserInfo(), reader) as Model.Golf_UserInfo);
                }
            }
            return userlist;
        }
        public int insert(string sql, params SqlParameter[] paramList)
        {
            return SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, paramList);
        }

        public int select(string sql)
        {
            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, new SqlParameter[0]).ToString());
        }

        public int getScalar(string sql)
        {           //sql语句的参数 如"insert into golf_Graffiti (pic,title,author) VALUES('qbfiqb','2222','3333');SELECT @@IDENTITY"
            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, new SqlParameter[0]).ToString());
        }

        public DataTable query(string sql)
        {
            DataSet data = new DataSet();
            using (SqlDataAdapter dsCommand = new SqlDataAdapter())
            {
                try
                {
                    dsCommand.SelectCommand = new SqlCommand();
                    dsCommand.SelectCommand.CommandText = sql;
                    dsCommand.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                    dsCommand.Fill(data);
                }
                catch (SqlException err)
                {
                    System.Console.WriteLine(err.Message);
                }
            }
            return data.Tables[0];
        }
        public int delete(string sql)
        {
            return SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, new SqlParameter[0]);
        }
    }
}
